package com.sdk.paas.core;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sdk.paas.conts.PaasConts;
import com.sdk.paas.core.enums.ErrorEnum;
import lombok.Data;

import java.util.Objects;

/**
 * 最基本的返回类型。code为0为成功，非0为失败，msg为操作信息
 *
 * @author willson
 */
@Data
public class CodeResult {

    private Integer code;

    private String msg;

    public static String fail(ErrorEnum errorEnum) {
        CodeResult result = new CodeResult();
        result.setCode(errorEnum.getKey());
        result.setMsg(errorEnum.getDesc());
        return JSONObject.toJSONString(result);
    }

    public static String fail(Integer code, String msg) {
        CodeResult result = new CodeResult();
        result.setCode(code);
        result.setMsg(msg);
        return JSONObject.toJSONString(result);
    }

    @JsonIgnore
    public boolean isSucceed() {
        return Objects.equals(PaasConts.SUCCESS_CODE, code);
    }

    @JsonIgnore
    public boolean isFailed() {
        return !isSucceed();
    }

}
